package com.hejun.bos.action.base;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import com.hejun.bos.common.base.BaseAction;
import com.hejun.bos.domain.base.Courier;
import com.hejun.bos.service.base.CourierService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@Controller
@Scope("prototype")
@Namespace("/courier")
@ParentPackage("json-default")
public class CourierAction extends BaseAction<Courier> {
	private static final long serialVersionUID = 1L;

	
	@Autowired
	private CourierService courierService;

	

	@Action(value = "findByPage", results = { @Result(name = "success", type = "json") })
	public String findByPage() {
		// 构造查询条件
		Specification<Courier> specification = new Specification<Courier>() {
			List<Predicate> list=new ArrayList<>();
			@Override
			/**
			 * JPQL查询root 对应表,cb对应查询参数
			 */
			public Predicate toPredicate(Root<Courier> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				if (StringUtils.isNotBlank(model.getCourierNum())) {
					Predicate p1 = cb.equal(root.get("courierNum").as(String.class), model.getCourierNum());
					list.add(p1);
				}
				if(StringUtils.isNotBlank(model.getCompany())){
					Predicate p2 = cb.like(root.get("company").as(String.class), "%"+model.getCompany()+"%");
					list.add(p2);
				}
				if(StringUtils.isNoneBlank(model.getType())){
					Predicate p3 = cb.equal(root.get("type").as(String.class), model.getType());
					list.add(p3);
				}
				//关联Standard表
				Join<Object, Object> standardRoot = root.join("standard");
				if(model.getStandard()!=null&&StringUtils.isNotBlank(model.getStandard().getName())){
					Predicate p4 = cb.like(standardRoot.get("name").as(String.class), "%"+model.getStandard().getName()+"%");
					list.add(p4);
				}
				return cb.and(list.toArray(new Predicate[0]));
			}
		};
		Pageable pageable = new PageRequest(page - 1, rows);
		Page<Courier> result = courierService.findAll(specification,pageable);
		changeResultToJson(result);
		return SUCCESS;
	}

	@Action(value = "save", results = {
			@Result(name = "success", type = "redirect", location = "/pages/base/courier.html") })
	public String save() {
		courierService.save(model);
		return SUCCESS;
	}

	private String ids;

	public void setIds(String ids) {
		this.ids = ids;
	}

	private String flag;

	public void setFlag(String flag) {
		this.flag = flag;
	}

	@Action(value = "update", results = {
			@Result(name = "success", type = "redirect", location = "/pages/base/courier.html") })
	public String update() {
		courierService.update(flag.toCharArray()[0], ids);
		return SUCCESS;
	}
}
